/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @return {number[]}
 */
// 暴力解法
// var nextLargerNodes = function(head) {
//     let temp = [], start = head
//     while(start) {
//         temp.push(start.val)
//         start = start.next
//     }
//     let res = []
//     for(let i = 0; i < temp.length; i++) {
//         for(let j = i + 1; j < temp.length; j++) {
//             if(temp[j] > temp[i]) {
//                 res.push(temp[j])
//                 break;
//             }
//         }
//         res.push(0)
//     }
//     return res
// };
/**
 * 单调栈
 */
var nextLargerNodes1 = function (head) {
    let stack = [];
    const list = []
    let i = 0;
    while (head) {
        while (stack.length !== 0 && stack[stack.length - 1][1] < head.val) {
            const temp = stack.pop();
            list.push([temp[0], head.val])
        }
        stack.push([i, head.val])
        i++;
        head = head.next
    }
    let res = new Array(i).fill(0)
    list.forEach(item => {
        res[item[0]] = item[1]
    })
    return res;
};
console.log(nextLargerNodes1([2, 1, 5]));